﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class VKKAD03
    {
        public VKKAD03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKADR();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            base.LOADBYAUF();
            if (ORDERNUM.Length < 19)
                return;
            string auftype = ORDERNUM.Substring(0, 1);
            switch (auftype)
            { 
                default:
                case "K":
                    KASA = "  ";
                    break;
                case "B":
                    KASA = "EK";
                    break;
            }
            dwData = TBMain.Clone().NewRow();
            dwData["VKAMANDANT"] = app.Mandant;
            dwData["VKASA"] = KASA;
            dwData["VKAAUFKRS"] = ORDERNUM.Substring(1, 2);
            dwData["VKAAUFLFD"] = ORDERNUM.Substring(3, 6);
            dwData["VKAAUFPOS"] = ORDERNUM.Substring(9, 3);
            SResult = ((VVKADR)MV).QueryVKADR(dwData, 1);
            MergeData(SResult, true, true);
            if (!IsEdit)
                return;
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count == 0)
            {
                PM["KASA"] = KASA;
                PM["KRS"] = ORDERNUM.Substring(1, 2);
                PM["LFD"] = ORDERNUM.Substring(3, 6);
                PM["POS"] = ORDERNUM.Substring(9, 3);

                ((VVKADR)MV).NEWVKADR(PM);
                vwMaster.View.MoveCurrentToFirst();
                if (dwMaster == null)
                    return;
                if (KDR == null) KDR = new VKUNDADR();
                if (TBDKDR == null)
                {
                    DataRow dwKDAD = KDR.TBMain.NewRow();
                    dwKDAD["KDAMANDANT"] = app.Mandant;
                    dwKDAD["KDAKUNDNR"] =" AND KDAKUNDNR='"+ ORDERNUM.Substring(11, 8)+"'";
                    SResult = KDR.QueryKUNDADR(dwKDAD, 1);
                    TBDKDR = SResult.Tables[0].Copy();
                }
                GetVKAddress(dwMaster, "VK");
                GetVVAddress(dwMaster, "VV");
                GetVVAddress(dwMaster, "VR");
                ((VVKADR)MV).UpdateVKADR(dwMaster, app.company, 0);
            }
            TBDKDR = null;
        }
    }
}
